<!-- eslint-disable vue/multi-word-component-names -->
<template>
    <div class="login_container">
        <el-row>
            <el-col :span="12" :xs="0"></el-col>
            <el-col :span="12" :xs="24">
                <el-form ref="loginForm" class="login_form" :model="loginFormData" :rules="rules">
                    <h1>Hello</h1>
                    <h2>欢迎来到硅谷甄选</h2>
                    <el-form-item prop="username">
                        <el-input :prefix-icon="User" v-model="loginFormData.username"></el-input>
                    </el-form-item>

                    <el-form-item prop="password">
                        <el-input type="password" :prefix-icon="Lock" :suffix-icon="Hide"
                            v-model="loginFormData.password"></el-input>
                    </el-form-item>

                    <el-form-item>
                        <el-button class="login_btn" type="primary" size="default" :loading="loading"
                            @click="login">登录</el-button>
                    </el-form-item>

                </el-form>
            </el-col>
        </el-row>
    </div>
</template>

<script setup lang="ts">
import useUserStore from '@/store/user';
import { getChTimePeriod } from '@/utils/time';
import { Hide, Lock, User } from '@element-plus/icons-vue';
import { ElNotification, type FormRules } from 'element-plus';
import type { FormInstance } from 'element-plus/lib/components/form/index.js';
import { ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
const route = useRoute();
const router = useRouter()
const userStore = useUserStore();

const loginForm = ref<FormInstance>()
const loading = ref(false);
const loginFormData = ref({ username: 'admin', password: 'atguigu123' });

const validateUsername = (rule: any, value: any, callback: any) => {
    if (value !== 'admin') {
        callback(new Error('用户名错误'))
    } else {
        callback()
    }
}
const rules = ref<FormRules>({
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { required: true, min: 5, max: 10, message: '用户需要大于6位,小于10位', trigger: 'change' },
        { validator: validateUsername, trigger: 'change' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
    ],
});



const login = async () => {
    await loginForm.value?.validate();
    try {
        loading.value = true;
        await userStore.login(loginFormData.value);
        let redirect = route.query.redirect;
        if (!redirect) {
            redirect = '/'
        }
        await router.push({path:redirect as string});

        ElNotification({
            type: 'success',
            title: `Hi,${getChTimePeriod()}好!`,
            message: '欢迎回来'
        })

    } catch (error) {
        ElNotification({ type: 'error', message: (error as Error).message })
    } finally {
        loading.value = false;
    }
};

</script>

<style scoped lang="scss">
.login_container {
    height: 100vh;
    width: 100%;
    background: url('@/assets/images/background.jpg') no-repeat;
    background-size: cover;

    .login_form {
        width: 70%;
        position: relative;
        top: 30vh;
        background: url('@/assets/images/login_form.png') no-repeat;
        background-size: cover;
        padding: 40px;

        h1 {
            color: white;
            font-size: 40px;
        }

        h2 {
            margin: 20px 0px;
            color: white;
            font-size: 20px;
        }

        .login_btn {
            width: 100%;
        }
    }
}
</style>@/store/user/user